//
//  ElementLayer.cpp
//  PlantNanny
//
//  Created by Joueu on 14-12-24.
//
//

#include "ElementLayer.h"
#include "Resource.h"

bool ElementLayer::initWith(const char * tit, const char * cont ,const char * iconName)
{
    if (!Layer::init()) {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    shaderLayer = ShaderLayer::create();
    this -> addChild(shaderLayer, 5000);
    
    popLayer = PopupLayer::create();
    this -> addChild(popLayer, 5001);
    
    auto bg = Sprite::create("edescription.png");
    bg -> setPosition(visibleSize * 0.5);
    popLayer -> addChild(bg);
    
    auto closeBtn = ui::Button::create("btn_close@2x.png");
    closeBtn -> setScale(1.2f);
    closeBtn -> setAnchorPoint(Point(0.3f, 0.7f));
    closeBtn -> addTouchEventListener(CC_CALLBACK_2(ElementLayer::closeBtnPressed, this));
    closeBtn -> setPosition(Point(bg -> getPositionX() - bg -> getContentSize().width * 0.5f, bg -> getPositionY() + bg -> getContentSize().height * 0.5f));
    popLayer -> addChild(closeBtn);
    
    auto spr = Sprite::create(iconName);
    spr -> setPosition(Point(bg -> getContentSize().width * 0.9f, bg -> getContentSize().height * 0.85));
    bg -> addChild(spr);
    
    auto title = Label::createWithSystemFont(tit, "微软雅黑", 40);
    title -> setTextColor(Color4B::BLACK);
    title -> enableOutline(Color4B(0,0,0,200));
    title -> setAnchorPoint(Point(0,0));
    title -> setPosition(Point(bg -> getContentSize().width * 0.1, bg -> getContentSize().height * 0.75));
    bg -> addChild(title);
    
    auto content = Label::createWithSystemFont(cont, "微软雅黑", 30);
    content -> setAnchorPoint(Point(0,1));
    content -> setTextColor(Color4B::GRAY);
    content -> setWidth(bg -> getContentSize().width * 0.8f);
    content -> setPosition(Point(bg -> getContentSize().width * 0.1, bg -> getContentSize().height * 0.65));
    bg -> addChild(content);
    
    auto listener = EventListenerTouchOneByOne::create();
    listener -> setSwallowTouches(true);
    listener -> onTouchBegan = [](Touch *touch, Event *event){
        auto target = static_cast<Sprite *>(event -> getCurrentTarget());
        Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
        Size s = target->getContentSize();
        Rect rect = Rect(0, 0, s.width, s.height);
        if(rect.containsPoint(locationInNode)){
            //这里return true swallowTouches才有效
            return true;
        }
        return false;
    };
    
    Director::getInstance() -> getEventDispatcher() -> addEventListenerWithSceneGraphPriority(listener, bg);
    
    
    
    shaderLayer -> setClickBgBlock([&](){
        PlayNegativeSound
        ActionInterval *action = ScaleTo::create(0.3f, 0.1f);
        CallFunc *func = CallFunc::create([&](){
            popLayer -> removeFromParentAndCleanup(true);
            shaderLayer -> removeFromParentAndCleanup(true);
        });
        
        popLayer -> runAction(Sequence::create(action, func, NULL));
        
    });
    return true;
}

ElementLayer *ElementLayer::createWith(const char * tit, const char * cont ,const char * iconName)
{
    ElementLayer *layer = new ElementLayer();
    if (layer && layer -> initWith(tit, cont, iconName)) {
        layer -> autorelease();
    }else {
        CC_SAFE_RELEASE(layer);
    }
    return layer;
}

void ElementLayer::closeBtnPressed(Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type == ui::Widget::TouchEventType::ENDED) {
        PlayNegativeSound
        ActionInterval *action = ScaleTo::create(0.3f, 0.1f);
        CallFunc *func = CallFunc::create([&](){
            popLayer -> removeFromParentAndCleanup(true);
            shaderLayer -> removeFromParentAndCleanup(true);
        });
        popLayer -> runAction(Sequence::create(action, func, NULL));
    }
}